package resoft.report;

import resoft.jeasyform.util.EnvUtil;

import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p></p>
 * User: albert
 * Date: Sep 3, 2007
 * Time: 1:31:25 AM
 */
public class JReport {
    public JReport(String fileName) throws IOException {
        String jformHome = EnvUtil.getJFormHome();
        String path = jformHome + "etc/" + fileName;
        LineNumberReader lnr = new LineNumberReader(new FileReader(path));
        String line = lnr.readLine();
        while(line!=null) {
            content.append(line);
            content.append('\n');
            line = lnr.readLine();
        }
        lnr.close();
    }

    /**
     * set variable
     *
     * @param key String
     * @param value object
     * */
    public void setVariable(String key,Object value) {
        variables.put(key,value);
    }

    /**
     * set backPage enable
     *
     * @param flag boolean true:enable back page;false:disable
     * */
    public void setBackPageEnabled(boolean flag) {
        this.backPageEnabled = flag;
    }

    public void print() {
        PrintUtil.startPrint();
        //set some variables
        for(Iterator itr = variables.keySet().iterator();itr.hasNext();) {
            String key = (String) itr.next();
            String value = (String) variables.get(key);
            if(value==null) {
                value = "";
            }
            key = "${" + key + "}";
            int index = content.indexOf(key);
            if(index<0) {
                continue;
            }
            int keyLength = 0;
            int valueLength = 0;
            try {
                keyLength = key.getBytes("GB2312").length;
                valueLength = value.getBytes("GB2312").length;
            } catch (UnsupportedEncodingException e) {

            }
            if(keyLength>valueLength) {
                for(int i = 0;i<keyLength - valueLength;i++) {
                    value += " ";
                    valueLength ++;
                }
            }

            content.replace(index,index + valueLength + 1,value);
        }
        //replace
        Matcher matcher = pattern.matcher(content);
        while(matcher.find()) {
            int startIndex = matcher.start();
            int endIndex = matcher.end();
            String value = "";
            for(int i = 0;i<endIndex - startIndex;i++) {
                value += " ";
            }
            content.replace(startIndex,endIndex,value);
        }


        PrintUtil.print(content.toString());
        if(backPageEnabled) {
            PrintUtil.backPage();
        }
        PrintUtil.endPrint();
    }

    private StringBuffer content = new StringBuffer();
    private Map variables = new HashMap();
    private boolean backPageEnabled = false;
    private Pattern pattern = Pattern.compile("\\$\\{\\w+\\}");
}
